package com.example.pkmedical.Fragments;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Activity.model.UserMessage;
import com.example.pkmedical.Activity.usermsg.NewPatientActivity;
import com.example.pkmedical.Base.BaseFragment;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.SoftKeyboardUtil;

import org.litepal.LitePal;
import org.litepal.LitePalApplication;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class UserMsgFragment extends BaseFragment {
    @BindView(R.id.user_name_edit)
    EditText userNameEdit;
    @BindView(R.id.user_age_edit)
    EditText userAgeEdit;
    @BindView(R.id.user_date_edit)
    EditText userDateEdit;
    @BindView(R.id.user_sex_edit)
    EditText userSexEdit;
    @BindView(R.id.user_height_edit)
    EditText userHeightEdit;
    @BindView(R.id.user_weight_edit)
    EditText userWeightEdit;
    @BindView(R.id.user_ok_bt)
    Button userOkBt;
    @BindView(R.id.user_ok_cancle)
    Button userOkCancle;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_usermsg;
    }

    @Override
    public void findViewById(View view) {

    }

    @Override
    public void setViewData(View view) {
        userNameEdit.setEnabled(false);
        userSexEdit.setEnabled(false);
        userAgeEdit.setEnabled(false);
        userHeightEdit.setEnabled(false);
        userDateEdit.setEnabled(false);
        userWeightEdit.setEnabled(false);
        SoftKeyboardUtil.hideSoftKeyboardFragmrnt(MyApp.getAppContext(),null);
        getData();
    }

    @Override
    public void setClickEvent(View view) {

    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if (hidden) {
        } else {
            //重新显示到最前端
            getData();
        }
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.USER_MESSAGE) {
            getData();
        }
    }

    @OnClick({R.id.user_ok_bt, R.id.user_ok_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_ok_bt:
                startActivity(new Intent(getContext(), NewPatientActivity.class));
//                getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                break;
            case R.id.user_ok_cancle:
                break;
        }
    }
    private void getData(){
        List<UserMessage> list = LitePal.findAll(UserMessage.class);
        if (list.size() > 0) {
            List<UserMessage> userMessage = LitePal.findAll(UserMessage.class);
            userNameEdit.setText(userMessage.get(0).getUserName());
            userSexEdit.setText(userMessage.get(0).getUserSex());
            userAgeEdit.setText(userMessage.get(0).getUserAge());
            userHeightEdit.setText(userMessage.get(0).getUserHeight());
            userDateEdit.setText(userMessage.get(0).getInDate());
            userWeightEdit.setText(userMessage.get(0).getUserWeight());
        }
    }
}
